﻿using System;
using System.ComponentModel;
using Lilium.Reflection;
using Lilium.Web.Mvc.EntityFilters;
using Microsoft.Practices.ObjectBuilder2;
using Microsoft.Practices.Unity;


namespace Lilium.Unity.MvcHttpApplication
{
	public sealed class EntityFilterReflectionPolicy: IEntityFilterConfigurationPolicy
	{
		public EntityFilterReflectionPolicy(IUnityContainer container)
		{
			if (container == null) throw new ArgumentNullException("container");

			m_Container = container;
		}

		#region IEntityFilterConfigurationPolicy Members

		public void ConfigureEntityFilter(IBuilderContext context)
		{
			if (context == null) throw new ArgumentNullException("context");

			var typeToReflect = context.BuildKey.Type;
			var descroiptors = typeToReflect.GetCustomAttributes<IEntityFilterDescriptor>();
			
			Container.RegisterInstance(descroiptors);
		}

		#endregion

		#region private Container Container

		private readonly IUnityContainer m_Container;

		private IUnityContainer Container
		{
			get
			{
				return m_Container;
			}
		}

		#endregion
	}
}